package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.EduService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class EduController {
    @Autowired
    EduService eduService;
    //1.1流量分时统计（新访客数）测试
    @RequestMapping("/isNew")
    public Object isNew(@RequestParam("nowDate")String nowDate ){
        List<Map> list = eduService.isNew(nowDate);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();

        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("time")) ;
        }
        dataObject.put("categories",catArray);

        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","新访客数");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("uid")) ;
        }
        serObject.put("data",dataArray);
        serArray.add(serObject);
        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //1.2答题情况统计测试
    @RequestMapping("/question")
    public Object question (@RequestParam("nowDate")String nowDate ){
        List<Map> list = eduService.question(nowDate);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();

        JSONArray colArray = new JSONArray();

        JSONObject colObject = new JSONObject();
        colObject.put("name","题目内容");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","正确答题次数");
        colObject1.put("id","module");
        colArray.add(colObject1);

        JSONObject colObject2 = new JSONObject();
        colObject2.put("name","答题次数");
        colObject2.put("id","business");
        colArray.add(colObject2);

        JSONObject colObject3 = new JSONObject();
        colObject3.put("name","正确率");
        colObject3.put("id","name");
        colArray.add(colObject3);

        dataObject.put("columns",colArray);

        JSONArray rowArray = new JSONArray();

        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("question"));
            rowObject.put("module",map.get("correct"));
            rowObject.put("business",map.get("count"));
            rowObject.put("name",map.get("correctLv"));
            rowArray.add(rowObject);
        }

        dataObject.put("rows",rowArray);


        jsonObject.put("data",dataObject);
        return jsonObject;
    };



    //1.3各试卷考试统计测试
    @RequestMapping("/parper")
    public Object parper(@RequestParam("nowDate")String nowDate ){
        List<Map> list = eduService.parper(nowDate);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();

        JSONArray colArray = new JSONArray();

        JSONObject colObject = new JSONObject();
        colObject.put("name","试卷名称");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","考试人次");
        colObject1.put("id","module");
        colArray.add(colObject1);

        JSONObject colObject2 = new JSONObject();
        colObject2.put("name","平均分");
        colObject2.put("id","business");
        colArray.add(colObject2);

        JSONObject colObject3 = new JSONObject();
        colObject3.put("name","平均用时");
        colObject3.put("id","name");
        colArray.add(colObject3);

        dataObject.put("columns",colArray);

        JSONArray rowArray = new JSONArray();

        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys",map.get("title"));
            rowObject.put("module",map.get("uid"));
            rowObject.put("business",map.get("avgScore"));
            rowObject.put("name",map.get("avgTime"));
            rowArray.add(rowObject);
        }

        dataObject.put("rows",rowArray);


        jsonObject.put("data",dataObject);
        return jsonObject;
    };
    //1.4各来源流量统计测试
    @RequestMapping("/source")
    public Object source(@RequestParam("nowDate")String nowDate ){
        List<Map> list = eduService.source(nowDate);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();

        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("source")) ;
        }
        dataObject.put("categories",catArray);

        JSONArray serArray = new JSONArray();

        JSONObject serObject = new JSONObject();
        serObject.put("name","独立访客数");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("uid")) ;
        }
        serObject.put("data",dataArray);
        serArray.add(serObject);

        JSONObject serObject1 = new JSONObject();
        serObject1.put("name","会话总数");
        JSONArray dataArray1 = new JSONArray();
        for (Map map : list) {
            dataArray1.add(map.get("session")) ;
        }
        serObject1.put("data",dataArray1);
        serArray.add(serObject1);

        JSONObject serObject2 = new JSONObject();
        serObject2.put("name","会话平均浏览页面数");
        JSONArray dataArray2 = new JSONArray();
        for (Map map : list) {
            dataArray2.add(map.get("page")) ;
        }
        serObject2.put("data",dataArray2);
        serArray.add(serObject2);

        JSONObject serObject3 = new JSONObject();
        serObject3.put("name","会话平均停留时长");
        JSONArray dataArray3 = new JSONArray();
        for (Map map : list) {
            dataArray3.add(map.get("time")) ;
        }
        serObject3.put("data",dataArray3);
        serArray.add(serObject3);

        JSONObject serObject4 = new JSONObject();
        serObject4.put("name","跳出率");
        JSONArray dataArray4 = new JSONArray();
        for (Map map : list) {
            dataArray4.add(map.get("skip")) ;
        }
        serObject4.put("data",dataArray4);
        serArray.add(serObject4);



        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };
}
